﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Contact Collector, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Security
Imports WillStrohl.Modules.GooglePlus.GooglePlusController

Namespace WillStrohl.Modules.GooglePlus

    Partial Public Class Settings
        Inherits WnsPortalModuleBase

#Region " Private Members "

        

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Not Page.IsPostBack Then
                    Me.BindData()
                End If
            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub lnkSubmit_Click(sender As Object, e As System.EventArgs) Handles lnkSubmit.Click
            If Me.Page.IsValid Then
                Me.UpdateSettings()
            End If

            Me.SendBackToModule()
        End Sub

        Private Sub lnkCancel_Click(sender As Object, e As System.EventArgs) Handles lnkCancel.Click
            Me.SendBackToModule()
        End Sub

#End Region

#Region " Private Helper Methods "

        ''' <summary>
        ''' This method centralizes the binding and object assignment for all settings
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub BindData()

            Try

                Me.LocalizeModule()
                Me.LoadSettings()

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex, True)
            End Try

        End Sub

        Private Sub LocalizeModule()

            Me.rfvClientId.ErrorMessage = Me.GetLocalizedString("rfvClientId.ErrorMessage")
            Me.rfvClientSecret.ErrorMessage = Me.GetLocalizedString("rfvClientSecret.ErrorMessage")
            Me.rfvApiKey.ErrorMessage = Me.GetLocalizedString("rfvApiKey.ErrorMessage")
            Me.rfvProfile.ErrorMessage = Me.GetLocalizedString("rfvProfile.ErrorMessage")
            Me.revProfile.ErrorMessage = Me.GetLocalizedString("revProfile.ErrorMessage")

            Me.lnkSubmit.Text = Me.GetLocalizedString("lnkSubmit.Text")
            Me.lnkCancel.Text = Me.GetLocalizedString("lnkCancel.Text")

        End Sub

        ''' <summary>
        ''' This is the inherited method that DNN uses to update the settings from the UI
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub UpdateSettings()
            Try

                Dim objModules As New DotNetNuke.Entities.Modules.ModuleController
                Dim sec As New DotNetNuke.Security.PortalSecurity

                If Not String.IsNullOrEmpty(Me.txtClientId.Text) Then
                    objModules.UpdateModuleSetting(Me.ModuleId, SETTINGKEY_CLIENTID, sec.InputFilter(Me.txtClientId.Text, PortalSecurity.FilterFlag.NoMarkup))
                End If

                If Not String.IsNullOrEmpty(Me.txtClientSecret.Text) Then
                    objModules.UpdateModuleSetting(Me.ModuleId, SETTINGKEY_CLIENTSECRET, sec.InputFilter(Me.txtClientSecret.Text, PortalSecurity.FilterFlag.NoMarkup))
                End If

                If Not String.IsNullOrEmpty(Me.txtApiKey.Text) Then
                    objModules.UpdateModuleSetting(Me.ModuleId, SETTINGKEY_APIKEY, sec.InputFilter(Me.txtApiKey.Text, PortalSecurity.FilterFlag.NoMarkup))
                End If

                If Not String.IsNullOrEmpty(Me.txtProfile.Text) Then
                    objModules.UpdateTabModuleSetting(Me.TabModuleId, SETTINGKEY_PROFILEURL, sec.InputFilter(Me.txtProfile.Text, PortalSecurity.FilterFlag.NoMarkup))
                End If

                Entities.Modules.ModuleController.SynchronizeModule(Me.ModuleId)

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        ''' <summary>
        ''' This is the inherited DNN method that loads saved settings into the settings UI
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub LoadSettings()

            Try
                
                If Not Me.Settings(SETTINGKEY_CLIENTID) Is Nothing Then
                    Me.txtClientId.Text = Me.Settings(SETTINGKEY_CLIENTID).ToString()
                End If

                If Not Me.Settings(SETTINGKEY_CLIENTSECRET) Is Nothing Then
                    Me.txtClientSecret.Text = Me.Settings(SETTINGKEY_CLIENTSECRET).ToString()
                End If

                If Not Me.Settings(SETTINGKEY_APIKEY) Is Nothing Then
                    Me.txtApiKey.Text = Me.Settings(SETTINGKEY_APIKEY).ToString()
                End If

                If Not Me.Settings(SETTINGKEY_PROFILEURL) Is Nothing Then
                    Me.txtProfile.Text = Me.Settings(SETTINGKEY_PROFILEURL).ToString()
                End If

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub

        Private Sub SendBackToModule()
            Response.Redirect(NavigateURL())
        End Sub

#End Region

    End Class

End Namespace